package com.site.wisdom_construction.controller.publicModule;

import com.alibaba.fastjson.JSONObject;
import com.site.wisdom_construction.entity.healthEpidemicModel.HealthCodeResult;
import com.site.wisdom_construction.responseEntity.OneDataResult;
import com.site.wisdom_construction.responseEntity.PersonResult;
import com.site.wisdom_construction.util.HttpRequest;
import com.site.wisdom_construction.util.imgUtil.AliyunOSSUtil;
import com.site.wisdom_construction.util.verification.NoToken;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileOutputStream;
import java.util.List;
import java.util.Map;

/**
 * @Author daiyifei @Date 2022/1/7 @Description
 */
@Api(tags = "阿里云oss图片接口")
@RestController
@RequestMapping("OssImg")
@CrossOrigin
public class OssUploadController {

  @ApiOperation(value = "人员采集图片上传", notes = "")
  @PostMapping("/personUpload")
  @NoToken
  public OneDataResult person(MultipartFile file) {
    String uploadUrl = null;
    try {

      if (null != file) {
        String filename = file.getOriginalFilename();
        if (!"".equals(filename.trim())) {
          File newFile = new File(filename);
          FileOutputStream os = new FileOutputStream(newFile);
          os.write(file.getBytes());
          os.close();
          file.transferTo(newFile);
          String Host = "healthCode";
          // 上传到OSS
          uploadUrl = AliyunOSSUtil.upload(newFile, Host);
          // 删除上传时缓存在服务器上的文件
          newFile.delete();
        }
      }
    } catch (Exception ex) {
      ex.printStackTrace();
    }

    OneDataResult oneDataResult = new OneDataResult();
    PersonResult personResult = new PersonResult();
    personResult.setUploadImg(uploadUrl);
    JSONObject jsonObject = new JSONObject();
    HttpRequest httpRequest = new HttpRequest();
    String path =
        "https://aip.baidubce.com/rest/2.0/solution/v1/iocr/recognise?access_token=24.fc998b8af37258cc8b6ddf3ae6967daf.2592000.1654741956.282335-25962476";
    jsonObject =
        httpRequest.StringDataJson(
            path, "url=" + uploadUrl + "&templateSign=id_card_front");
    JSONObject jsonObjectL = new JSONObject();
    jsonObjectL = (JSONObject) jsonObject.get("data");
    List list = (List) jsonObjectL.get("ret");
    for (int i = 0; i < list.size(); i++) {
      Map map1 = (Map) list.get(i);
      String s = (String) map1.get("word_name");
      if (s.equals("姓名")) {
        personResult.setName((String) map1.get("word"));
      } else if (s.equals("民族")) {
        personResult.setNation((String) map1.get("word"));
      } else if (s.equals("住址")) {
        personResult.setAddress((String) map1.get("word"));
      } else if (s.equals("公民身份号码")) {
        personResult.setCard((String) map1.get("word"));
      } else if (s.equals("出生")) {
        personResult.setTime((String) map1.get("word"));
      } else if (s.equals("性别")) {
        personResult.setSex((String) map1.get("word"));
      }
    }
    oneDataResult.setData(personResult);
    return oneDataResult;
  }

  @ApiOperation(value = "人员图片上传", notes = "")
  @PostMapping("/personImgUpload")
  @NoToken
  public String personImgUpload(MultipartFile file) {

    try {

      if (null != file) {
        String filename = file.getOriginalFilename();
        if (!"".equals(filename.trim())) {
          File newFile = new File(filename);
          FileOutputStream os = new FileOutputStream(newFile);
          os.write(file.getBytes());
          os.close();
          file.transferTo(newFile);
          String Host = "personImg";
          // 上传到OSS
          String uploadUrl = AliyunOSSUtil.upload(newFile, Host);
          // 删除上传时缓存在服务器上的文件
          newFile.delete();
          return uploadUrl;
        }
      }
    } catch (Exception ex) {
      ex.printStackTrace();
    }
    return "upload";
  }

  @ApiOperation(value = "安全图片上传", notes = "")
  @PostMapping("/safetyImgUpload")
  @NoToken
  public String safetyImgUpload(MultipartFile file) {

    try {

      if (null != file) {
        String filename = file.getOriginalFilename();
        if (!"".equals(filename.trim())) {
          File newFile = new File(filename);
          FileOutputStream os = new FileOutputStream(newFile);
          os.write(file.getBytes());
          os.close();
          file.transferTo(newFile);
          String Host = "safetyImg";
          // 上传到OSS
          String uploadUrl = AliyunOSSUtil.upload(newFile, Host);
          // 删除上传时缓存在服务器上的文件
          newFile.delete();
          return uploadUrl;
        }
      }
    } catch (Exception ex) {
      ex.printStackTrace();
    }
    return "upload";
  }

  @ApiOperation(value = "安全多图上传", notes = "")
  @PostMapping("/safetyImgUploadMore")
  @NoToken
  public String safetyImgUploadMore(MultipartFile[] files) {
    String Url = "";
    for (MultipartFile file : files) {
      try {

        if (null != file) {
          String filename = file.getOriginalFilename();
          if (!"".equals(filename.trim())) {
            File newFile = new File(filename);
            FileOutputStream os = new FileOutputStream(newFile);
            os.write(file.getBytes());
            os.close();
            file.transferTo(newFile);
            String Host = "safetyImg";
            // 上传到OSS
            String uploadUrl = AliyunOSSUtil.upload(newFile, Host);
            // 删除上传时缓存在服务器上的文件
            newFile.delete();
            Url += uploadUrl + ",";
          }
        }
      } catch (Exception ex) {
        ex.printStackTrace();
      }
    }
    String returnUrl = Url.substring(0, Url.length() - 1);
    return returnUrl;
  }

  @ApiOperation(value = "质量图片上传", notes = "")
  @PostMapping("/qualityImgUpload")
  @NoToken
  public String qualityImgUpload(MultipartFile file) {

    try {

      if (null != file) {
        String filename = file.getOriginalFilename();
        if (!"".equals(filename.trim())) {
          File newFile = new File(filename);
          FileOutputStream os = new FileOutputStream(newFile);
          os.write(file.getBytes());
          os.close();
          file.transferTo(newFile);
          String Host = "qualityImg";
          // 上传到OSS
          String uploadUrl = AliyunOSSUtil.upload(newFile, Host);
          // 删除上传时缓存在服务器上的文件
          newFile.delete();
          return uploadUrl;
        }
      }
    } catch (Exception ex) {
      ex.printStackTrace();
    }
    return "upload";
  }

  @ApiOperation(value = "质量多图上传", notes = "")
  @PostMapping("/qualityImgUploadMore")
  @NoToken
  public String qualityImgUploadMore(MultipartFile[] files) {
    String Url = "";
    for (MultipartFile file : files) {
      try {

        if (null != file) {
          String filename = file.getOriginalFilename();
          if (!"".equals(filename.trim())) {
            File newFile = new File(filename);
            FileOutputStream os = new FileOutputStream(newFile);
            os.write(file.getBytes());
            os.close();
            file.transferTo(newFile);
            String Host = "qualityImg";
            // 上传到OSS
            String uploadUrl = AliyunOSSUtil.upload(newFile, Host);
            // 删除上传时缓存在服务器上的文件
            newFile.delete();
            Url += uploadUrl + ",";
          }
        }
      } catch (Exception ex) {
        ex.printStackTrace();
      }
    }
    String returnUrl = Url.substring(0, Url.length() - 1);
    return returnUrl;
  }

  @ApiOperation(value = "附件上传", notes = "")
  @PostMapping("/accessoryUpload")
  @NoToken
  public String accessoryUpload(MultipartFile file) {

    try {

      if (null != file) {
        String filename = file.getOriginalFilename();
        if (!"".equals(filename.trim())) {
          File newFile = new File(filename);
          FileOutputStream os = new FileOutputStream(newFile);
          os.write(file.getBytes());
          os.close();
          file.transferTo(newFile);
          String Host = "accessory";
          // 上传到OSS
          String uploadUrl = AliyunOSSUtil.upload(newFile, Host);
          // 删除上传时缓存在服务器上的文件
          newFile.delete();
          return uploadUrl;
        }
      }
    } catch (Exception ex) {
      ex.printStackTrace();
    }
    return "upload";
  }

  @ApiOperation(value = "培训课程图片", notes = "")
  @PostMapping("/accessoryTrainingImg")
  @NoToken
  public String accessoryTrainingImg(MultipartFile file) {

    try {

      if (null != file) {
        String filename = file.getOriginalFilename();
        if (!"".equals(filename.trim())) {
          File newFile = new File(filename);
          FileOutputStream os = new FileOutputStream(newFile);
          os.write(file.getBytes());
          os.close();
          file.transferTo(newFile);
          String Host = "trainingImg";
          // 上传到OSS
          String uploadUrl = AliyunOSSUtil.upload(newFile, Host);
          // 删除上传时缓存在服务器上的文件
          newFile.delete();
          return uploadUrl;
        }
      }
    } catch (Exception ex) {
      ex.printStackTrace();
    }
    return "upload";
  }

  @ApiOperation(value = "培训课程视频", notes = "")
  @PostMapping("/accessoryTrainingVideo")
  @NoToken
  public String accessoryTrainingVideo(MultipartFile file) {

    try {

      if (null != file) {
        String filename = file.getOriginalFilename();
        if (!"".equals(filename.trim())) {
          File newFile = new File(filename);
          FileOutputStream os = new FileOutputStream(newFile);
          os.write(file.getBytes());
          os.close();
          file.transferTo(newFile);
          String Host = "trainingVideo";
          // 上传到OSS
          String uploadUrl = AliyunOSSUtil.upload(newFile, Host);
          // 删除上传时缓存在服务器上的文件
          newFile.delete();
          return uploadUrl;
        }
      }
    } catch (Exception ex) {
      ex.printStackTrace();
    }
    return "upload";
  }

  @ApiOperation(value = "培训课程PDF", notes = "")
  @PostMapping("/accessoryTrainingPdf")
  @NoToken
  public String accessoryTrainingPdf(MultipartFile file) {

    try {

      if (null != file) {
        String filename = file.getOriginalFilename();
        if (!"".equals(filename.trim())) {
          File newFile = new File(filename);
          FileOutputStream os = new FileOutputStream(newFile);
          os.write(file.getBytes());
          os.close();
          file.transferTo(newFile);
          String Host = "trainingPdf";
          // 上传到OSS
          String uploadUrl = AliyunOSSUtil.upload(newFile, Host);
          // 删除上传时缓存在服务器上的文件
          newFile.delete();
          return uploadUrl;
        }
      }
    } catch (Exception ex) {
      ex.printStackTrace();
    }
    return "upload";
  }

  @ApiOperation(value = "商品图片上传", notes = "")
  @PostMapping("/commodityImg")
  @NoToken
  public String commodityImg(MultipartFile file) {

    try {

      if (null != file) {
        String filename = file.getOriginalFilename();
        if (!"".equals(filename.trim())) {
          File newFile = new File(filename);
          FileOutputStream os = new FileOutputStream(newFile);
          os.write(file.getBytes());
          os.close();
          file.transferTo(newFile);
          String Host = "commodity";
          // 上传到OSS
          String uploadUrl = AliyunOSSUtil.upload(newFile, Host);
          // 删除上传时缓存在服务器上的文件
          newFile.delete();
          return uploadUrl;
        }
      }
    } catch (Exception ex) {
      ex.printStackTrace();
    }
    return "upload";
  }

  @ApiOperation(value = "党建图片上传", notes = "")
  @PostMapping("/partyConstructionPhoto")
  @NoToken
  public String partyConstructionPhoto(MultipartFile file) {

    try {

      if (null != file) {
        String filename = file.getOriginalFilename();
        if (!"".equals(filename.trim())) {
          File newFile = new File(filename);
          FileOutputStream os = new FileOutputStream(newFile);
          os.write(file.getBytes());
          os.close();
          file.transferTo(newFile);
          String Host = "partyConstructionPhoto";
          // 上传到OSS
          String uploadUrl = AliyunOSSUtil.upload(newFile, Host);
          // 删除上传时缓存在服务器上的文件
          newFile.delete();
          return uploadUrl;
        }
      }
    } catch (Exception ex) {
      ex.printStackTrace();
    }
    return "upload";
  }

  @ApiOperation(value = "党建人员图片上传", notes = "")
  @PostMapping("/partyConstructionPersonImg")
  @NoToken
  public String partyConstructionPersonImg(MultipartFile file) {

    try {

      if (null != file) {
        String filename = file.getOriginalFilename();
        if (!"".equals(filename.trim())) {
          File newFile = new File(filename);
          FileOutputStream os = new FileOutputStream(newFile);
          os.write(file.getBytes());
          os.close();
          file.transferTo(newFile);
          String Host = "partyConstructionPerson";
          // 上传到OSS
          String uploadUrl = AliyunOSSUtil.upload(newFile, Host);
          // 删除上传时缓存在服务器上的文件
          newFile.delete();
          return uploadUrl;
        }
      }
    } catch (Exception ex) {
      ex.printStackTrace();
    }
    return "upload";
  }

  @ApiOperation(value = "党建视频上传", notes = "")
  @PostMapping("/partyConstructionVideo")
  @NoToken
  public String partyConstructionVideo(MultipartFile file) {

    try {

      if (null != file) {
        String filename = file.getOriginalFilename();
        if (!"".equals(filename.trim())) {
          File newFile = new File(filename);
          FileOutputStream os = new FileOutputStream(newFile);
          os.write(file.getBytes());
          os.close();
          file.transferTo(newFile);
          String Host = "partyConstructionVideo";
          // 上传到OSS
          String uploadUrl = AliyunOSSUtil.upload(newFile, Host);
          // 删除上传时缓存在服务器上的文件
          newFile.delete();
          return uploadUrl;
        }
      }
    } catch (Exception ex) {
      ex.printStackTrace();
    }
    return "upload";
  }

  @ApiOperation(value = "里程碑图片上传", notes = "")
  @PostMapping("/progressMilestonesImg")
  @NoToken
  public String progressMilestones(MultipartFile file) {

    try {

      if (null != file) {
        String filename = file.getOriginalFilename();
        if (!"".equals(filename.trim())) {
          File newFile = new File(filename);
          FileOutputStream os = new FileOutputStream(newFile);
          os.write(file.getBytes());
          os.close();
          file.transferTo(newFile);
          String Host = "progressMilestonesImg";
          // 上传到OSS
          String uploadUrl = AliyunOSSUtil.upload(newFile, Host);
          // 删除上传时缓存在服务器上的文件
          newFile.delete();
          return uploadUrl;
        }
      }
    } catch (Exception ex) {
      ex.printStackTrace();
    }
    return "upload";
  }

  @ApiOperation(value = "签名图片", notes = "")
  @PostMapping("/signatureImg")
  @NoToken
  public String signatureImg(MultipartFile file) {

    try {

      if (null != file) {
        String filename = file.getOriginalFilename();
        if (!"".equals(filename.trim())) {
          File newFile = new File(filename);
          FileOutputStream os = new FileOutputStream(newFile);
          os.write(file.getBytes());
          os.close();
          file.transferTo(newFile);
          String Host = "signature";
          // 上传到OSS
          String uploadUrl = AliyunOSSUtil.upload(newFile, Host);
          // 删除上传时缓存在服务器上的文件
          newFile.delete();
          return uploadUrl;
        }
      }
    } catch (Exception ex) {
      ex.printStackTrace();
    }
    return "upload";
  }

  @ApiOperation(value = "入岗教育内容图片", notes = "")
  @PostMapping("/signatureRecordImg")
  @NoToken
  public String signatureRecordImg(MultipartFile file) {

    try {

      if (null != file) {
        String filename = file.getOriginalFilename();
        if (!"".equals(filename.trim())) {
          File newFile = new File(filename);
          FileOutputStream os = new FileOutputStream(newFile);
          os.write(file.getBytes());
          os.close();
          file.transferTo(newFile);
          String Host = "signatureRecord";
          // 上传到OSS
          String uploadUrl = AliyunOSSUtil.upload(newFile, Host);
          // 删除上传时缓存在服务器上的文件
          newFile.delete();
          return uploadUrl;
        }
      }
    } catch (Exception ex) {
      ex.printStackTrace();
    }
    return "upload";
  }

  @ApiOperation(value = "防疫信息身份证照片上传", notes = "")
  @PostMapping("/antiepidemicPersonImg")
  @NoToken
  public String antiepidemicPersonImg(MultipartFile file) {

    try {

      if (null != file) {
        String filename = file.getOriginalFilename();
        if (!"".equals(filename.trim())) {
          File newFile = new File(filename);
          FileOutputStream os = new FileOutputStream(newFile);
          os.write(file.getBytes());
          os.close();
          file.transferTo(newFile);
          String Host = "antiepidemicPerson";
          // 上传到OSS
          String uploadUrl = AliyunOSSUtil.upload(newFile, Host);
          // 删除上传时缓存在服务器上的文件
          newFile.delete();
          return uploadUrl;
        }
      }
    } catch (Exception ex) {
      ex.printStackTrace();
    }
    return "upload";
  }
}
